package com.ebeijia.controller;

import com.ebeijia.dto.ApplyWaterDto;
import com.ebeijia.mybatis.pojo.ApplyInfo;
import com.ebeijia.myenum.ApplyWaterSteteEnum;
import com.ebeijia.query.ApplyQuery;
import com.ebeijia.query.Paginator;
import com.ebeijia.service.admin.ApplyInfoService;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.web.ResponseMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YPJ on 2016/5/16.
 */
@Controller
@RequestMapping("/work")
public class ApplyInfoController {
    @Resource
    private ApplyInfoService applyInfoService;

    @RequestMapping("/toApplyWater.htm")
    public ModelAndView toApply() {
        ModelAndView modelAndView = new ModelAndView("work/applyWaterList");
        Map<String, Object> map = new HashMap<>();
        for (ApplyWaterSteteEnum e : ApplyWaterSteteEnum.values()) {
            map.put(e.getCode(), e.getText());
        }
        modelAndView.addObject("applyState", map);
        return modelAndView;
    }

    @RequestMapping("/applyWater.json")
    @ResponseBody
    public ResponseMessage applyList(ApplyQuery applyQuery) {
        Paginator<ApplyWaterDto> paginator = new Paginator<ApplyWaterDto>(applyQuery,
            applyInfoService.queryApplyDetail(applyQuery), applyInfoService.queryApplyDetailCount(applyQuery));
        return ResponseMessage.success(paginator);

    }

    @RequestMapping("/updateApplyState.json")
    @ResponseBody
    public ResponseMessage editApplyState(String applyNo, String state) {
        ApplyInfo applyInfo = applyInfoService.selectByParimaryApplyNo(applyNo);
        Map<String, Object> map = new HashMap<>();
        map.put("applyNo", applyNo);
        map.put("state", state);
        if (applyInfo.getApplyStatus().equals(state)) {
            return ResponseMessage.error("201", "当前状态没有变更");
        }
        if (applyInfo.getApplyStatus().equals(ApplyWaterSteteEnum.submit.getCode())
            && state.equals(ApplyWaterSteteEnum.wait_send.getCode())) {
            //当前状态为待受理，更改为已受理
            applyInfoService.updateStateByApplyNo(map);
            return ResponseMessage.success();

        } else if (applyInfo.getApplyStatus().equals(ApplyWaterSteteEnum.wait_send.getCode())
            && state.equals(ApplyWaterSteteEnum.success.getCode())) {
            //当前状态为已受理,更改为已完成
            applyInfoService.updateStateByApplyNo(map);
            return ResponseMessage.success();

        } else {
            return ResponseMessage.error("202", "状态不允许修改");
        }
    }

    @RequestMapping("/toApplyQueryList.htm")
    public ModelAndView toApplyQueryList() {
        ModelAndView modelAndView = new ModelAndView("work/applyWaterQueryList");
        Map<String, Object> map = new HashMap<>();
        for (ApplyWaterSteteEnum e : ApplyWaterSteteEnum.values()) {
            map.put(e.getCode(), e.getText());
        }
        modelAndView.addObject("applyState", map);
        return modelAndView;
    }

    @RequestMapping("/applyQueryList.json")
    @ResponseBody
    public ResponseMessage applyQueryList(ApplyQuery applyQuery) {
        Paginator<ApplyWaterDto> paginator = new Paginator<ApplyWaterDto>(applyQuery,
            applyInfoService.selectApplyList(applyQuery), applyInfoService.selectApplyListCount(applyQuery));
        return ResponseMessage.success(paginator);
    }
}
